/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class frmLogin extends javax.swing.JFrame {

    /**
     * Creates new form frmLogin
     */
    public frmLogin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfUsername = new javax.swing.JTextField();
        tfPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("ĐĂNG NHẬP");

        jLabel2.setText("Username");

        jLabel3.setText("Password");

        btnLogin.setText("Đăng nhập");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnLogin)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)
                                .addComponent(tfUsername)
                                .addComponent(tfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 311, Short.MAX_VALUE)))))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(2, 2, 2)
                .addComponent(tfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnLogin)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Ham kiem tra dang nhap 
    private int kiemTraDangNhap() {
        //Lay thong tin username va password tu form
        String userName = tfUsername.getText();
        String password = tfPassword.getText();
        //Goi ham lay nguoi dung theo username
        NguoiDung nd = NguoiDung.layNguoiDungTheoUsername(userName);
        //Kiem tra neu nguoi dung khac null thi kiem tra tiep password
        if (nd != null) {
            //Neu password nguoi dung = password nhap thi dang nhap thanh cong
            if (nd.getPassword().equals(password)) {
                //Gan nguoi dung vao bien toan cuc de su dung cho man hinh sau khi dang nhap
                Main.nguoiDungDangNhap = nd;
                //Tra ve loai nguoi dung tuong ung
                return nd.getLoaiNguoiDung();
            }

        }
        //Neu dang nhap khong thanh cong thi tra ve -1
        return -1;
    }

    //Su kien bam nut login
    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:
        //gan gia tri kiem tra dang nhap vao bien kt
        int kt = kiemTraDangNhap();
        //Neu kt>=0 thi dang nhap thanh cong
        if (kt >= 0) {
            //Hien thi thong bao dang nhap thanh cong
            JOptionPane.showMessageDialog(null, "Đăng nhập thành công ", "Thông báo", JOptionPane.PLAIN_MESSAGE);
            //Neu kt=1 thi nguoi dung la Admin
            if (kt == 1) {
                //Hien thi man hinh quan ly nguoi dung
                frmQLNguoiDung frm = new frmQLNguoiDung();
                frm.setVisible(true);

            } //Neu kt=2 thi nguoi dung la giao vien
            else if (kt == 2) {
                //Hien thi man hinh quan ly de thi
                frmQLDeThi frm = new frmQLDeThi();
                frm.setVisible(true);
            } //Nguoi dung con lai la sinh vien
            else {
                //Hien thi man hinh thi trac nghiem
                frmChonDeThi frm = new frmChonDeThi();
                frm.setVisible(true);
            }
            //Thoat man hinh login
            this.dispose();
        } else {
            //Neu sai ten dang nhap hoac mat khau thi thong bao
            JOptionPane.showMessageDialog(null, "Sai tên đăng nhập hoặc mật khẩu", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_btnLoginActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JTextField tfUsername;
    // End of variables declaration//GEN-END:variables
}
